package com.sun.base;

import java.util.ArrayList;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sun.bata.LotterData;
import com.sun.lotter.R;

public class Ball extends FrameLayout implements OnClickListener {
	private boolean mChecked = false;
	public static final int REDBALL = 0;
	public static final int BULEBALL = 2;
	public static final int YELLOWBALL = 4;
	public static final int ZODIAC = 8;
	public static final int DXDS = 16;
	public static int BallWith = 30;
	private int[] resid = new int[2];
	private int colors[] = new int[2];
	private int balltype;
	TextView ball;

	static int z[] = new int[] { 0, R.drawable.z1, R.drawable.z2,
			R.drawable.z3, R.drawable.z4, R.drawable.z5, R.drawable.z6,
			R.drawable.z7, R.drawable.z8, R.drawable.z9, R.drawable.z10,
			R.drawable.z11, R.drawable.z12 };
	static int z2[] = new int[] { 0, R.drawable.z21, R.drawable.z22,
			R.drawable.z23, R.drawable.z24, R.drawable.z25, R.drawable.z26,
			R.drawable.z27, R.drawable.z28, R.drawable.z29, R.drawable.z210,
			R.drawable.z211, R.drawable.z212 };
	private ArrayList<String> list;
int lotterID;
int playID;
	public Ball(Context context, int balltype, ArrayList<String> list, int resID,int lotterID,int playID) {
		super(context);
		this.playID=playID;
		ball = new TextView(getContext());
		this.lotterID = lotterID;
		setId(resID);
		this.balltype = balltype;
		setBackgroundDrawable(balltype);
		this.setOnClickListener(this);
		LayoutParams lp = new FrameLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		ball.setLayoutParams(lp);
		this.addView(ball, 30, 30);
		ball.setGravity(Gravity.CENTER);
		this.list = list;
	}

	public void setBackgroundDrawable(int balltype) {
		switch (balltype) {
		case REDBALL:
			resid[0] = R.drawable.redball_2;
			resid[1] = R.drawable.redball_1;
			colors[1] = getContext().getResources()
					.getColor(R.color.white_grey);
			colors[0] = getContext().getResources().getColor(R.color.white);
			break;
		case BULEBALL:
			colors[1] = getContext().getResources()
					.getColor(R.color.white_grey);
			colors[0] = getContext().getResources().getColor(R.color.white);
			resid[0] = R.drawable.buleball_2;
			resid[1] = R.drawable.buleball_1;
			break;

		case ZODIAC:
			resid[0] = z[ball.getId()];
			resid[1] = z2[ball.getId()];
			colors[0] = getContext().getResources().getColor(R.color.black);
			colors[1] = getContext().getResources().getColor(R.color.black);
			break;
		case DXDS:
		case YELLOWBALL:
		default:
			if (lotterID == LotterData.FREQLOTT
					|| lotterID == LotterData.SELECT5IN11) {
				resid[0] = R.drawable.squar_3;
				resid[1] = R.drawable.squar_1;
			} else {
				resid[0] = R.drawable.yellowball2;
				resid[1] = R.drawable.yellowball1;
			}
			colors[0] = getContext().getResources().getColor(R.color.black);
			colors[1] = getContext().getResources().getColor(R.color.black);
			break;
		}
		setBackgroundDrawable(getResources().getDrawable(
				isChecked() ? resid[1] : resid[0]));
		ball.setTextColor(isChecked() ? colors[0] : colors[1]);
	}

	public boolean isChecked() {
		return mChecked;
	}

	public void setChecked(boolean checked) {
		this.mChecked = checked;
		setBackgroundDrawable(getResources().getDrawable(
				isChecked() ? resid[1] : resid[0]));
		ball.setTextColor(isChecked() ? colors[0] : colors[1]);
	}

	public String getBallID() {
		return ball.getText().toString();
	}

	private static String dxds[] = new String[] { "��", "С", "��", "˫" };

	public void setBallLable(String lable) {
		if (balltype == DXDS) {
			ball.setText(dxds[ball.getId()]);
		} else if (balltype == ZODIAC) {
			ball.setText("");
		} else
			ball.setText(lable);
	}

	@Override
	public int getId() {
		// TODO Auto-generated method stub
		return ball.getId();
	}

	@Override
	public void setId(int id) {
		ball.setId(id);
		super.setId(id);
	}

	@Override
	public void onClick(View arg0) {
		setChecked(!isChecked());
		setBackgroundDrawable(getResources().getDrawable(
				isChecked() ? resid[1] : resid[0]));
//		play(getContext(), isChecked() ? R.raw.ball_key_down
//				: R.raw.ball_key_up);
		if (lotterID == LotterData.FREQLOTT && playID == 5) {
			list.clear();
			list.add("" + ball.getId());
		} else if (isChecked()) {
			list.add(ball.getId() + "");
		} else {
			list.remove(ball.getId() + "");
			list.remove("0" + ball.getId());
		}
//		((ChooseBall) getContext()).accountStakes();
	}

	public Ball getBall(int ballid) {
		Ball _ball = null;
		if (ball.getId() == ballid) {
			_ball = this;
		}
		return _ball;
	}

	@Override
	public boolean isClickable() {
		// TODO Auto-generated method stub
		return super.isClickable();
	}

	public void setMClickable(boolean clickable) {
		if (!clickable) {
			ball.setTextColor(getResources().getColor(R.color.grey));
		
			if (lotterID == LotterData.FREQLOTT
					|| lotterID == LotterData.SELECT5IN11) 
				setBackgroundDrawable(getResources().getDrawable(
						R.drawable.squar_2));
			 else 
			
			setBackgroundDrawable(getResources().getDrawable(
					R.drawable.grid_ball));
		} else {
			setBackgroundDrawable(getResources().getDrawable(
					isChecked() ? resid[1] : resid[0]));
			ball.setTextColor(isChecked() ? colors[0] : colors[1]);
		}
		super.setClickable(clickable);
	}
}
